package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.dao.impl.UserImpl;
import com.cn.wanxi.dto.UserDto;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.IUserService;
import redis.clients.jedis.Jedis;

public class UserServiceImpl implements IUserService {
    @Override
    public ResultModel add(UserDto userDto) {
        ResultModel resultModel = new ResultModel();
//        注册之前需要进行验证码的判断
        if (code(userDto)) {
//            需要进行用户名的重复判断
            if (findByUsernameIsHave(userDto.getUserModel())) {
                resultModel.setResult("当前用户名已经存在，请重新输入");
            } else {
                UserDao userDao = new UserImpl();
                resultModel.setCode(userDao.add(userDto.getUserModel()));
            }

        } else {
            resultModel.setResult("验证码不正确，请重新输入");
        }
        return resultModel;
    }

    @Override
    public String Login(UserDto userDto) {
        if (code(userDto)) {
//            需要判断用户名和密码是否输入正确
            if (findByUsernameAndPasswordIsHave(userDto.getUserModel())) {
                return "成功登陆";
            } else {
                return "用户名或者密码错误";
            }
        } else {
            return "验证码不正确，请重新输入";
        }

    }

    @Override
    public ResultModel findAll(UserDto userDto, PageModel pageModel) {
        ResultModel resultModel = new ResultModel();

        UserDao userDao = new UserImpl();
        resultModel.setObject(userDao.findAll(userDto, pageModel));
        resultModel.setCount(count(userDto));
        return resultModel;
    }

    /**
     * 根据查询条件查询总条数
     * @param userDto
     * @return
     */
    private int count(UserDto userDto) {
        UserDao userDao = new UserImpl();
        return userDao.count(userDto);
    }

    @Override
    public int del(int id) {
        UserDao userDao = new UserImpl();
        return userDao.del(id);
    }

    @Override
    public ResultModel update(UserDto userDto) {
        ResultModel resultModel = new ResultModel();
        if (code(userDto)) {
            UserDao userDao = new UserImpl();
            resultModel.setCode(userDao.update(userDto.getUserModel()));
        } else {
            resultModel.setResult("验证码不正确");
        }
        return resultModel;
    }

    @Override
    public UserModel findByUserId(int id) {
        UserDao userDao = new UserImpl();
        return userDao.findByUserId(id);
    }

    /**
     * 判断验证码是否正确
     *
     * @param userDto
     * @return
     */
    private boolean code(UserDto userDto) {
        return userDto.getCode().equals(userDto.getSessionCode());
    }

    /**
     * 如果存在，则返回true，不存在，返回false
     *
     * @param userModel
     * @return
     */
    private boolean findByUsernameIsHave(UserModel userModel) {
        UserDao userDao = new UserImpl();
        UserModel userModel1 = userDao.findByUsernameIsHave(userModel);
        return userModel1 != null;
    }

    /**
     * 根据用户名和密码去数据库查询，是否存在，如果存在，返回true，不存在返回false
     *
     * @param userModel
     * @return
     */
    private boolean findByUsernameAndPasswordIsHave(UserModel userModel) {
        UserDao userDao = new UserImpl();
        UserModel userModel1 = userDao.findByUsernameAndPasswordIsHave(userModel);
        return userModel1 != null;
    }
}
